package com.android.pagemarket.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.pagemarket.business.logic.InitManager;
import com.android.pagemarket.io.protocol.in.HttpAction.ActionListener;
import com.android.pagemarket.io.protocol.in.TransQueryAction;
import com.android.pagemarket.io.protocol.out.QueryList;
import com.android.pagemarket.io.protocol.out.QueryList.OrderItem;
import com.android.pagemarket.ui.widget.OrderItemView;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-13
 * @see : 订单查询列表适配器
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class OrderListAdapter extends DomoreListAdapter<OrderItem> implements DataLoaderHandler<OrderItem>
{
    private TransQueryAction mAction;
    
    public OrderListAdapter(Context context)
    {
        super(context);
    }
    
    public OrderListAdapter(Context context, ListView listView)
    {
        super(context, listView, null);
        setDataLoaderHandler(this);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        if(convertView == null)
        {
            convertView = new OrderItemView(mContext);
        }
        OrderItemView view = (OrderItemView)convertView;
        OrderItem item = (OrderItem)getItem(position);
        view.bindData(item);
        return view;
    }

    public TransQueryAction loadData()
    {
        if(!InitManager.getInstance().isLogin()) return null;
        
        String name = InitManager.getInstance().mLoginCompany.companyName;
        String num  = InitManager.getInstance().mLoginCompany.companyNum;
        // 没有其它则默认查询左右订单
        mAction = new TransQueryAction(name,num);
        mAction.setPage(1, 10);
        // 还有参数则查询输入参数
        mAction.setActionListener(new ActionListener<QueryList>()
        {
            @Override
            public void onSucceed(QueryList result)
            {
                if(result.isNoError())
                {
                    if(mAction.isFirst())
                    {
                        reload();
                    }
                    onLoaded(result.list);
                }else
                {
                    showLoadError(result.resultInfo, true);
                }
            }
            
            @Override
            public void onError(int resultCode)
            {
                OrderListAdapter.this.onError(true);
            }
        });
        reloadAction();
        return mAction;
    }
    
    public void reloadAction()
    {
        reload();
        showLoading();
        mAction.submit();
    }
    
    @Override
    public int getMaxItems()
    {
        return 1000;
    }

    @Override
    public void getNext(DataLoaderHandler.DataLoadedCallback<OrderItem> callback)
    {
        mAction.setNextPage();
        mAction.submit();
    }

    @Override
    public boolean isLoading()
    {
        return false;
    }

    @Override
    public boolean haveNext()
    {
        return true;
    }
}